<template>
  <div class="article-container">
    <van-nav-bar class="app-nav-bar" left-arrow @click-left="$router.back()" />
    <!-- 标题 -->
    <h1 class="title">{{ article.title }}</h1>
    <!-- 作者详情 -->
    <van-cell center class="user-info">
      <van-image class="avatar" fit="cover" slot="icon" round
        src="https://img.yzcdn.cn/vant/cat.jpeg" />
      <div class="name" slot="title">{{ article.aut_name }}</div>
      <div class="pudate" slot="label">{{ article.pubdate | relativeTime }}</div>
      <van-button class="follow-btn" :icon="article.is_followed? '':'plus'"
        :type="article.is_followed ? 'default' : 'info'" round size="small"
        @click="article.is_followed = !article.is_followed">
        {{ article.is_followed? '已关注':'关注' }}
      </van-button>
    </van-cell>
    <!-- 文章详情 -->
    <div class="markdown-body" ref="article-content">
      <img class="img" v-if="article.cover.b" :src="article.cover.b.images[0]" />
      <div v-if="article.cover.c">
        <div v-for="(img, index) in article.cover.c.images" :key="index">
          <img class="img" :src="img" />
        </div>
      </div>
      <div v-html="article.article_content"></div>
    </div>
    <!-- 底部区域 -->
    <div class="article-bottom">
      <van-button class="comment-btn" type="default" round size="small">写评论</van-button>
    </div>
  </div>
</template>

<script>
import './github-markdown.css'
import { ImagePreview } from 'vant'

export default {
  name: 'ArticleIdex',
  components: {},
  props: {
    articleId: {
      type: Number,
      required: true
    },
    article: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      images: [] // 存放预览图片
    }
  },
  // 计算属性
  computed: {},
  // 监控data中的数据变化
  watch: {},
  methods: {
    // 初始化数组方法1
    initImages () {
      if (this.article.cover.b) {
        console.log()
        this.images = [...this.article.cover.b.images]
      } else if (this.article.cover.c) {
        this.images = [...this.article.cover.c.images]
      }
    },
    //  初始化数组方法2
    initImages2 () {
      // 获取文章内容 DOM 容器
      this.$nextTick(() => {
        this.handleImagePreview()
      })
    },
    // 处理图片预览
    handleImagePreview () {
      const articleContent = this.$refs['article-content']
      // 得到所有的img标签
      const imgs = articleContent.querySelectorAll('img')
      // 每张图片的地址
      const imgPaths = []
      // 循环列表，给每张图片注册点击事件
      imgs.forEach((img, index) => {
        imgPaths.push(img.src)
        img.onclick = function () {
          // 调用 vant 组件中的 ImagePreview 方法
          ImagePreview({
            images: imgPaths,
            startPosition: index
          })
        }
      })
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    // 调用初始化数组
    // this.initImages()
    this.initImages2()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () { },
  beforeCreate () { }, // 生命周期 - 创建之前
  beforeMount () { }, // 生命周期 - 挂载之前
  beforeUpdate () { }, // 生命周期 - 更新之前
  updated () { }, // 生命周期 - 更新之后
  beforeDestroy () { }, // 生命周期 - 销毁之前
  destroyed () { }, // 生命周期 - 销毁完成
  activated () { } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style scoped lang='less'>
.article-container {
  .title {
    font-size: 20px;
    color: #3a3a3a;
    padding: 14px;
    background-color: #fff;
    margin: 0;
  }
  .user-info {
    .name {
      font-size: 14px;
      color: #333;
    }
    .pubdate {
      font-size: 11px;
      color: #b4b4b4;
    }
    .avatar {
      width: 35px;
      height: 35px;
      margin-right: 8px;
    }
    .follow-btn {
      width: 85px;
      height: 29px;
    }
  }
  .markdown-body {
    padding: 14px;
    background-color: #fff;
    text-align: center;
    .img {
      padding: 5px;
    }
  }
}
</style>
